package com.study.zouchao.superproject.comtasklist.fragment.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.TintTypedArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.study.zouchao.superproject.R;
import com.study.zouchao.superproject.base_zou.ZouImgLoader;
import com.study.zouchao.superproject.comtasklist.fragment.model.entity.TaskListResult;
import com.study.zouchao.superproject.travellist.adapter.TravelRecyclerViewAdapter;
import com.study.zouchao.superproject.travellist.entity.TravelListResult;
import com.study.zouchao.superproject.util.LogLongUtil;

import java.util.List;

/**
 * Created by Administrator on 2017/6/20.
 */

public class TaskListAdapter extends RecyclerView.Adapter<TaskListAdapter.MyViewHolder> {
    private List<TaskListResult.TaskItemBean> mDatas;
    private Context mContext;
    private IRvItemClickListener mListener;
    private View mFooterView;
    private static final int TYPE_FOOTER = 1;  //说明是带有Footer的
    private static final int TYPE_NORMAL = 2;  //说明是不带有header和footer的
    public TaskListAdapter(Context context, List<TaskListResult.TaskItemBean> datas) {
        mContext = context;
        mDatas = datas;
    }
    public void setOnItemClickListener(IRvItemClickListener listener) {
        mListener = listener;
    }
    @Override
    public TaskListAdapter.MyViewHolder  onCreateViewHolder(ViewGroup parent, int viewType) {
        if(mFooterView != null && viewType == TYPE_FOOTER){
            return new TaskListAdapter.MyViewHolder(mFooterView);
        }
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_task_list, parent, false);
        return new TaskListAdapter.MyViewHolder(view);
    }

    public void setFooterView(View footerView) {
        mFooterView = footerView;
        notifyItemInserted(getItemCount() - 1);
    }

    @Override
    public void onBindViewHolder(TaskListAdapter.MyViewHolder holder, int position) {
        if(getItemViewType(position) == TYPE_NORMAL){
            if(holder != null) {
                //这里加载数据的时候要注意，是从position-1开始，因为position==0已经被header占用了
                TaskListResult.TaskItemBean item = mDatas.get(position);
                ZouImgLoader.loadImage(mContext, holder.ivHeadimg, item.getSponsorHeadimg(), R.drawable.head_img);
                holder.tvNickname.setText(item.getSponsorNickName());
                LogLongUtil.logI("没有昵称？", item.toString());
                holder.tvHelp.setText("帮什么忙："+item.getHelpContent()+"");
                holder.tvReward.setText("薪资福利："+item.getReward()+"");
            }
        }
    }

    //返回View中Item的个数，这个时候，总的个数应该是ListView中Item的个数加上HeaderView和FooterView
    @Override
    public int getItemCount() {
        if(mFooterView == null) return mDatas.size();
        else return mDatas.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == getItemCount() - 1){
            //最后一个,应该加载Footer
            return TYPE_FOOTER;
        }
        return TYPE_NORMAL;
    }

    /**
     * Holder
     */
    class MyViewHolder extends RecyclerView.ViewHolder {
        View mItemView;
        ImageView ivHeadimg;
        TextView tvNickname, tvHelp, tvReward;

        public MyViewHolder(View itemView) {
            super(itemView);
            mItemView = itemView;
            ivHeadimg = (ImageView) itemView.findViewById(R.id.id_iv_headimg);
            tvNickname = (TextView) itemView.findViewById(R.id.id_tv_nickname);
            tvHelp = (TextView) itemView.findViewById(R.id.id_tv_help);
            tvReward = (TextView) itemView.findViewById(R.id.id_tv_reward);

            mItemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null) {
                        if (mDatas.size()-1 < getAdapterPosition()) return;
                        if (getAdapterPosition() < 0)   return;
                        //TODO:bug 越界15  -1
                        TaskListResult.TaskItemBean item = mDatas.get(getAdapterPosition());
                        mListener.onItemClick(mItemView, item.getTaskId());
                    }
                }
            });
        }

    }
    public interface IRvItemClickListener {
        void onItemClick(View v, int taskId);
    }
}
